package com.dwxt.cdms.service;

import com.alibaba.fastjson.JSON;
import com.dwxt.cdms.base.Page;
import com.dwxt.cdms.dao.CustomerMapper;
import com.dwxt.cdms.dao.HealthReportMapper;
import com.dwxt.cdms.dao.HealthWarnRecordMapper;
import com.dwxt.cdms.entity.Customer;
import com.dwxt.cdms.entity.HealthMessage;
import com.dwxt.cdms.entity.HealthReport;
import com.dwxt.cdms.service.wx.WxMessageService;
import com.dwxt.cdms.util.RunUtils;
import com.dwxt.common.base.BaseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class HealthReportService {

    @Autowired
    private HealthReportMapper healthReportMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private HealthWarnRecordMapper healthWarnRecordMapper;
    @Autowired
    private WxMessageService wxMessageService;

    public BaseResult page(Page page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<HealthReport> healthReportList = healthReportMapper.select(null);
        PageInfo pageInfo = new PageInfo(healthReportList);
        return BaseResult.ok(pageInfo);
    }


    public BaseResult select(HealthReport healthReport) {
        try {

            List<HealthReport> list = healthReportMapper.select(healthReport);
            if (list == null || list.size() == 0) {
                return BaseResult.ok();
            }
            HealthReport healthReport1 = list.get(0);
            Customer customer = customerMapper.selectByPrimaryKey(healthReport1.getCustomerId());
            healthReport1.setName(customer.getName() == null ? "" : customer.getName());
            return BaseResult.ok(healthReport1);
        } catch (Exception e) {
            return BaseResult.ok();
        }
    }

    public void createHealthReport() {
        List<Customer> customers = customerMapper.selectCustomerList(null);
        for (Customer customer : customers) {
            Integer customerId = customer.getId();
            Map heartMap = healthReportMapper.selectHeart(customerId);
            Map temperatureMap = healthReportMapper.selecttemperature(customerId);
            Long steps = healthReportMapper.selectSteps(customerId);
            String distance = RunUtils.getDistanceByStep(steps==null?0:steps);
            heartMap.putAll(temperatureMap);
            HealthReport healthReport = JSON.parseObject(JSON.toJSONString(heartMap), HealthReport.class);
            healthReport.setCustomerId(customerId);
            healthReport.setSteps(steps);
            healthReport.setDistance(distance);
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH,-1);
            int month = calendar.get(Calendar.MONTH)+1;
            int year = calendar.get(Calendar.YEAR);
            healthReport.setYear(Long.valueOf(year));
            healthReport.setMonth(Long.valueOf(month));
            healthReportMapper.insertSelective(healthReport);
            //小程序消息
            StringBuffer sb = new StringBuffer();
            sb.append("尊敬的老师您好：广电安博馨24小时守护中心监测到您").append(month).append("月的心率数据共计").append(healthReport.getHeartCount()).append("条，其中异常心率数据共计")
                    .append(healthReport.getHeartExceptionCount()).append("条！体温数据共计").append(healthReport.getTemperatureCount()).append("条，其中异常体温数据共计")
                    .append(healthReport.getTemperatureExceptionCount()).append("条;运动总步数").append(healthReport.getSteps()).append("步，共步行").append(healthReport.getDistance())
                    .append("公里;根据以上健康数据统计，心率数据在").append(healthReport.getHeartMin()).append("-").append(healthReport.getHeartMax()).append("，体温数据在")
                    .append(healthReport.getTemperatureMin()).append("-").append(healthReport.getTemperatureMax()).append("区间，请您保持心情愉悦，不要过度劳累，低盐低脂，饮食清淡，多吃新鲜瓜果蔬菜，适当运动。");
            String content = sb.toString();
            HealthMessage msg = new HealthMessage();
            msg.setType("月度健康报告通知");
            msg.setCustomerId(customer.getId());
            msg.setId(UUID.randomUUID().toString());
            String name = customer.getName() == null ? "" : customer.getName();
            msg.setContent(content);
            msg.setCrateTime(new Date());
            healthWarnRecordMapper.insertMessage(msg);
            //微信公众号消息
            wxMessageService.sendMonthReportToGzh(healthReport, customer);
        }
    }
}
